<?php
	session_start();
	set_time_limit(0);
	include_once('init.php');
	try {
	$m  = new Mongo();
	} catch(Exception $e) {
	  echo "连接数据库失败:".$e->getMessage();	
	}
	$db = $m->selectDB('foobar');
	$collection = $db->selectCollection("test");
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>PHP 操作 mongodb 数据库 by 此题无解</title>
		<meta charset="UTF-8">
		<meta name=description content="">
		<meta name=viewport content="width=device-width, initial-scale=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!-- Bootstrap CSS -->
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
	</head>
	<body>
	<h1 class="text-center">PHP 操作 mongodb 数据库(修改器)</h1>
	<div class="container">
		<div class="row">
		1. 插入10W测试数据
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'insert10W.txt');
			echo $s;
		?>
		</pre>
		<?php
			$count = $collection->count();
			runtime(0);
			if (isset($_GET['insert']) || $count<100){
				$collection->remove();
				for ($i=0; $i < 100000; $i++) { 
					$data = array(
						'foo'=>'bar',
						'baz'=>$i,
						'z'=>10-$i,
						);
					$collection->insert($data);
				}
			}
			
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php echo '要强制生成数据加参数?insert=true<br>数据量:'.$collection->count().' <br>运行时间:'.runtime(1);?></pre>
		</div>

		2. $inc 找到baz为100的数据，将z增加100
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'inc.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$cursor = $collection->find(array('baz'=>100));
			$data = $cursor->getNext();
			$id = $data['_id'];
			$result .= '修改前:z='.$data['z'].'<br>';
			$collection->update(array('baz'=>100), array('$inc'=>array('z'=>100)));
			$cursor = $collection->find(array('_id'=>$id));
			$result .= '修改后:z='.$cursor->getNext()['z'].'<br>';
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php echo $result.' <br>运行时间:'.runtime(2);?></pre>
		</div>		

		3. $set 用来指定一个键的值，如果不存在就创建<br>
		   找到baz为100的数据，指定name = ctwj
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'set.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$cursor = $collection->find(array('baz'=>100));
			$data = $cursor->getNext();
			$name = $data['name'];
			$result .= '修改前:name='.$name.'<br>';
			$collection->update(array('baz'=>100), array('$set'=>array('name'=>$name.'ctwj')));
			$cursor = $collection->find(array('baz'=>100));
			$result .= '修改后:name='.$cursor->getNext()['name'].'<br>';
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php echo $result.' <br>运行时间:'.runtime(3);?></pre>
		</div>	

		4. 使用$set 将baz为100的数据，指定name = 为一个数组 {first:ct,second:wj}
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'set1.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$cursor = $collection->find(array('baz'=>100));
			$data = $cursor->getNext();
			$name = $data['name'];
			$result .= '修改前:name='.$name.'<br>';
			$collection->update(array('baz'=>100), array('$set'=>array('name'=>array('first'=>'ct','second'=>'wj'))));
			$cursor = $collection->find(array('baz'=>100));
			$result .= '修改后:name='.join(',',$cursor->getNext()['name']).'<br>';
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php echo $result.' <br>运行时间:'.runtime(3);?></pre>
		</div>	

		5. 使用$unset 将baz为100的数据name节点删除
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'unset.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$cursor = $collection->find(array('baz'=>100));
			$predata = $cursor->getNext();
			$collection->update(array('baz'=>100), array('$unset'=>array('name'=>array('first'=>'ct','second'=>'wj'))));
			$cursor = $collection->find(array('baz'=>100));
			$nextdata = $cursor->getNext();
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php echo '修改前:';var_dump($predata);echo '修改后:数据为';var_dump($nextdata);echo ' <br>运行时间:'.runtime(3);?></pre>
		</div>	

		6. $inc 找到baz为100的数据，将z减8
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'dec.txt');
			echo $s;
		?>
		</pre>
		<?php
			runtime(0);
			$result = '';
			$cursor = $collection->find(array('baz'=>100));
			$data = $cursor->getNext();
			$id = $data['_id'];
			$result .= '修改前:z='.$data['z'].'<br>';
			$collection->update(array('baz'=>100), array('$inc'=>array('z'=>-8)));
			$cursor = $collection->find(array('_id'=>$id));
			$result .= '修改后:z='.$cursor->getNext()['z'].'<br>';
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php echo $result.' <br>运行时间:'.runtime(2);?></pre>
		</div>	


		<div><blockquote><em>php</em>操作<em>mongodb</em>数据库入门篇(2)<br>
		<a href="third.php">下一篇</a>
		<small class="pull-right"><cite>此题无解</cite></small></blockquote></div>

		</div>
	</div>

		<!-- jQuery -->
		<script src="./js/jquery.min.js"></script>
		<!-- Bootstrap JavaScript -->
		<script src="./js/bootstrap.min.js"></script>
		<!-- prettify -->
		<script src="./js/prettify.js"></script>
		<script>
		$(function(){
			prettyPrint();
		});
		</script>
	</body>
</html>